{% extends "base.html" %}

{% block content %}
  <h1>Loading...</h1>

  <div class="help-404" style="display:none;">
    <p>
      If you followed a link here, we'd appreciate it if you would open a
      <a href="https://github.com/mathesar-foundation/mathesar/issues">
        GitHub issue
      </a>
      describing how you got here so we can fix the link.
    </p>
    <p>
      You can also ask the Mathesar team for help by reaching out on
      <a href="https://wiki.mathesar.org/community/matrix/">Matrix</a>.
    </p>
  </div>
{% endblock %}

{% block scripts %}
  {{ super() }}
  <script>
    // @ts-check
    
    const defaultVersionAlias = 'latest';

    /**
     * Add strings here if your docs URL has any prefixes before the version
     * such as a repo name, a subdirectory, or a language code.
     * 
     * If your docs URLs look like `https://example.com/docs/en/1.0/foo/bar`
     * then set this value to `['docs', 'en']`.
     */
    const pathPartsBeforeVersion = [];
    const versionPositionInPath = pathPartsBeforeVersion.length;
    
    /** @returns {Promise<string | undefined>} */
    async function getRedirectionTarget() {
      try {
        const response = await fetch('/versions.json');
        if (!response.ok) return undefined;
        
        /** @type {({ version: string; title: string; aliases: string[]; })[]} */
        const versionBlobs = await response.json();
        const versionLikeStrings = versionBlobs.flatMap(v => [v.version, ...v.aliases]);
  
        const url = new URL(window.location.href);
        const pathParts = url.pathname.split('/').filter(Boolean);
        const firstPathPart = pathParts[versionPositionInPath];

        if (versionLikeStrings.includes(firstPathPart)) return undefined;

        return [
          '',
          ...pathPartsBeforeVersion,
          defaultVersionAlias,
          ...pathParts,
          url.search + url.hash
        ].join('/');
      }
      catch (e) {
        return undefined;
      }
    }

    function setupErrorPageContent() {
      const h1 = document.querySelector('h1');
      if (h1) {
        h1.textContent = 'Page Not Found';
      }

      /** @type {HTMLElement | null} */
      const help404 = document.querySelector('.help-404');
      if (help404) {
        help404.style.display = 'block';
      }
    }

    async function init() {
      const redirectionTarget = await getRedirectionTarget();
      if (redirectionTarget) {
        window.location.href = redirectionTarget;
      } else {
        setupErrorPageContent();
      }
    }

    init();
  </script>
{% endblock %}